/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "Lock.h"
#include <pthread.h>

namespace liba
{
namespace threads
{

#ifndef IPHONE_VERSION
class Mutex::Impl
{
	mutable pthread_mutex_t mutex;
	mutable pthread_mutexattr_t attributes;
public:
	Impl()
	{
		pthread_mutexattr_init( &attributes );
		pthread_mutexattr_settype( &attributes, PTHREAD_MUTEX_RECURSIVE );
		pthread_mutex_init( &mutex, &attributes );
	}
	~Impl()
	{
		pthread_mutex_destroy( &mutex );
		pthread_mutexattr_destroy( &attributes );
	}
	void lock_object()const
	{
		pthread_mutex_lock( &mutex );
	}
	void unlock_object()const
	{
		pthread_mutex_unlock( &mutex );
	}
};
#else
class Mutex::Impl
{
public:
	Impl()
	{
	}
	~Impl()
	{
	}
	void lock_object()const
	{
	}
	void unlock_object()const
	{
	}
};
#endif

void Mutex::lock_object()const
{
	impl->lock_object();
}

void Mutex::unlock_object()const
{
	impl->unlock_object();
}

Mutex::Mutex()
:	impl( new Impl )
{}

Mutex::~Mutex()
{
	delete impl;
}

} //namespace threads
} //namespace liba